/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import model.Professor;
import model.QuestaoProfessor;

/**
 *
 * @author edilson
 */
public class QuestaoProfessorJpaController {

    public QuestaoProfessorJpaController() {
        emf = DaoManager.getEmf();
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(QuestaoProfessor questaoProfessor) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(questaoProfessor);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(QuestaoProfessor questaoProfessor) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            questaoProfessor = em.merge(questaoProfessor);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = questaoProfessor.getId();
                if (findQuestaoProfessor(id) == null) {
                    throw new NonexistentEntityException("The questaoProfessor with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuestaoProfessor questaoProfessor;
            try {
                questaoProfessor = em.getReference(QuestaoProfessor.class, id);
                questaoProfessor.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The questaoProfessor with id " + id + " no longer exists.", enfe);
            }
            em.remove(questaoProfessor);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<QuestaoProfessor> findQuestaoProfessorEntities() {
        return findQuestaoProfessorEntities(true, -1, -1);
    }

    public List<QuestaoProfessor> findQuestaoProfessorEntities(int maxResults, int firstResult) {
        return findQuestaoProfessorEntities(false, maxResults, firstResult);
    }

    private List<QuestaoProfessor> findQuestaoProfessorEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from QuestaoProfessor as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public QuestaoProfessor findQuestaoProfessor(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(QuestaoProfessor.class, id);
        } finally {
            em.close();
        }
    }

    public int getQuestaoProfessorCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from QuestaoProfessor as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<QuestaoProfessor> findQuestaoProfessor(Professor professor) throws Exception {
        EntityManager em = getEntityManager();
        try {
            Query query = em.createQuery("SELECT p FROM QuestaoProfessor p WHERE p.professor = :professor");

            query.setParameter("professor", professor);

            return query.getResultList();

        } catch (Exception e) {
            throw new Exception();
        }
    }

    public int getNumeroQuestoes(Professor professor) throws Exception {
        return findQuestaoProfessor(professor).size();
    }

    public List<QuestaoProfessor> findQuestao(String stringPesquisa) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT p FROM QuestaoProfessor p WHERE p.questao.enunciado like :stringPesquisa");
            q.setParameter("stringPesquisa", "%" + stringPesquisa + "%");
            return q.getResultList();
        } finally {
            em.close();
        }
    }
        public List<QuestaoProfessor> findQuestao(String stringPesquisa, Professor professor) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT p FROM QuestaoProfessor p WHERE p.questao.enunciado like :stringPesquisa and p.professor =:professor");
            q.setParameter("stringPesquisa", "%" + stringPesquisa + "%");
            q.setParameter("professor", professor);
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
